﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Xml;

namespace CMTVision.Widgets.Framework
{
    public class BaseWidget : System.Web.UI.UserControl, CMTVision.Widgets.Framework.IWidget
    {
        #region Implement IWidget Interface

        public long WidgetID
        {
            get;
            set;
        }
         
        public bool IsInEditting
        {
            get
            {
                return Convert.ToBoolean(ViewState["IsInEditting"] ?? false);
            }
            set
            {
                ViewState["IsInEditting"] = value;
            }
        }
        
        public IWidgetContainer WidgetContainer
        {
            get;
            protected set;
        }

        public string State
        {
            get
            {
                return (ViewState["State"] ?? String.Empty).ToString();
            }
            set
            {
                ViewState["State"] = value;
            }
        }
        
        public virtual void Initialize()
        {
            using (CMTVision.Model.CMTWidgetsEntities entity = CMTVision.Common.EntitiesManager.LoadWidgetEntity())
            {
                var widget = (from w in entity.Widgets.Include("Template") where w.ID == WidgetID select w).FirstOrDefault();
                if (widget != null)
                {
                    State = widget.State; 
                }
            }
        }

        public virtual void Edit()
        {
            IsInEditting = true;
        }

        public virtual void Save()
        {
            using (CMTVision.Model.CMTWidgetsEntities entity = CMTVision.Common.EntitiesManager.LoadWidgetEntity())
            {
                var widget = (from w in entity.Widgets.Include("Template") where w.ID == WidgetID select w).FirstOrDefault();
                if (widget != null)
                {
                    widget.State = State;
                    entity.SaveChanges();
                }
            }
            IsInEditting = false;
        }

        public virtual void Refresh()
        { 
        }


        public virtual void Delete()
        {
            bool result = false;
            using (CMTVision.Model.CMTWidgetsEntities entity = CMTVision.Common.EntitiesManager.LoadWidgetEntity())
            {
                var widget = (from w in entity.Widgets.Include("Template") where w.ID == WidgetID select w).FirstOrDefault();
                if (widget == null)
                {
                    result = true;
                }
                else
                {
                    var templateid = widget.Template.ID;
                    entity.DeleteObject(widget);
                    var afterThisWidgets = (from w in entity.Widgets
                                            where w.ColumnID == widget.ColumnID
                                                && w.Template.ID == templateid
                                                && w.OrderNo > widget.OrderNo
                                            select w).ToList();
                    afterThisWidgets.ForEach(w => { w.OrderNo--; });
                    result = entity.SaveChanges() > 0;
                }
            } 
        }


        public virtual void InitContainer(IWidgetContainer container)
        {
            WidgetContainer = container;
        }

        #endregion

    }
}